package com.wk.search;

/**
 * @author admin
 * @date 2021/10/26 13:40
 */
public class TwoFenSearch {
    public static void main(String[] args) {
        System.out.println(search(new int[]{1,2,3,7,8,9},10));
    }

    /**
     * 二分查找
     * @param rows 升序
     * @param target 目标值
     * @return
     */
    public static int search(int[] rows,int target){
        int start = 0;
        int length = rows.length;
        int end = length-1;
        while (start <= end){
            int middle = (start+end)/2;
            if(rows[middle] < target){
                start = middle+1;
            }else if(rows[middle] > target){
                end = middle-1;
            }else {
                return middle;
            }
        }
        return -1;
    }
}
